package com.lyw.idempotentstarter.config;

import com.lyw.idempotentstarter.bean.IdempotentStorageService;
import com.lyw.idempotentstarter.bean.impl.LocalizeIdempotentStorageService;
import com.lyw.idempotentstarter.bean.impl.MysqlIdempotentStorageService;
import com.lyw.idempotentstarter.bean.impl.RedisIdempotentStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@EnableConfigurationProperties(TokenProperties.class)
@Configuration
public class TokenAspectAutoConfiguration {
    private final TokenProperties tokenProperties;
    @Autowired
    public TokenAspectAutoConfiguration(TokenProperties tokenProperties) {
        this.tokenProperties = tokenProperties;
    }

    @Bean
    public IdempotentStorageService idempotentStorageService(){
        TokenProperties.StorageTarget storageTarget = tokenProperties.getStorageTarget();
        switch (storageTarget){
            case REDIS:return new RedisIdempotentStorageService();
            case MYSQL:return new MysqlIdempotentStorageService();
            case LOCALIZE:return new LocalizeIdempotentStorageService();
        }
        return null;
    }

}
